package cn.itcast.itcaststore.web.servlet.client;

import cn.itcast.itcaststore.domain.Products;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;
/*
* 删除指定商品
* */
@WebServlet("/changeCart")
public class ChangeCartServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.得到商品id
        String id=request.getParameter("id");
        //2.得到要修改的数量
        int count=Integer.parseInt(request.getParameter("count"));
        //3.从session中获取购物车
        HttpSession session=request.getSession();
        Map<Products,Integer> cart=(Map<Products,Integer>)session.getAttribute("cart");
        Products p=new Products();
        p.setId(id);
        //修改购物车中指定的商品数量，如果count为0，表示删除该商品
        if (count!=0){
            cart.put(p,count);
        }else {
            cart.remove(p);
        }
        response.sendRedirect(request.getContextPath()+"/client/cart.jsp");
    }
}
